package com.sfpay.coreplatform.order.service.composite.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sfpay.coreplatform.account.common.enums.ExpenseFlag;
import com.sfpay.coreplatform.account.common.enums.TallyType;
import com.sfpay.coreplatform.account.valueobject.dto.Transfer;
import com.sfpay.coreplatform.order.common.enums.OptType;
import com.sfpay.coreplatform.order.common.enums.PayAcctType;
import com.sfpay.coreplatform.order.common.enums.PayType;
import com.sfpay.coreplatform.order.common.enums.RemitMethod;
import com.sfpay.coreplatform.order.common.enums.SourcesCode;
import com.sfpay.coreplatform.order.common.enums.tradeStatus;
import com.sfpay.coreplatform.order.common.util.TaskHandleManger;
import com.sfpay.coreplatform.order.persistence.dao.IPayOrderDao;
import com.sfpay.coreplatform.order.service.composite.IAsyncHandle;
import com.sfpay.coreplatform.order.service.config.HandelConfig;
import com.sfpay.coreplatform.order.service.proxy.account.IAccountMgrProxyService;
import com.sfpay.coreplatform.order.valueobject.dto.PayRequest;
import com.sfpay.coreplatform.order.valueobject.tmo.PayOrder;

@Service
public class AsyncHandle implements IAsyncHandle {

	@Resource
	private HandelConfig handelConfig;

	// 支付dao
	@Resource
	private IPayOrderDao payOrderDao;
	 
	//账务处理
	@Resource
	private IAccountMgrProxyService accountService;

	@Override
	public void cancelAccHandel(PayRequest request, String tradeNo, String payNo) {

	 

			// 组装支付订单
			PayOrder payOrder = new PayOrder();
			payOrder.setOrderNo(tradeNo);
			payOrder.setPayNo(payNo);// 订单号暂用以前的 主要作为参数传递 如果保存数据要重新生成
			payOrder.setOptType(OptType.PAY);
			payOrder.setPayStatus(tradeStatus.PROCESSING);
			payOrder.setPayAmt(-request.getPayAmt());//金额为负
			payOrder.setPayeeAcctNo(request.getPayeeAcctNo());
			payOrder.setPayeeMemberNo(request.getPayerMemberNo());
			payOrder.setTradeTallyCode(request.getTradePostNo());
			payOrder.setPayTallyCode(request.getPayPostNo());
			payOrder.setBusinessSn(request.getOutTradeNo());
			payOrder.setOrderType(request.getOrderType());
			payOrder.setPaySource(SourcesCode.OTHER);
			payOrder.setPayType(PayType.ACCOUNT);
			payOrder.setBusinessSource(SourcesCode.OTHER);
			payOrder.setPaySource(SourcesCode.OTHER);
			payOrder.setPayerAcctType(PayAcctType.ACCOUNT);
			payOrder.setPayerAcctNo(request.getPayerAcctNo());
			payOrder.setPayeeAcctType(PayAcctType.ACCOUNT);
			payOrder.setPayeeAcctNo(request.getPayeeAcctNo());
			payOrder.setRemitMethod(RemitMethod.FAST);
			payOrder.setRemark("消费被冲正");

			// 组装销账对象 没有支付交易号 
			Transfer transfer = new Transfer();
			transfer.setAccountPasswd(request.getPayerPassword());
			transfer.setAmount(-request.getPayAmt());//金额为负
			transfer.setBusinessSN(request.getOutTradeNo());
			transfer.setExpenseFlag(ExpenseFlag.PAYMENT);
			transfer.setMemberNo(request.getPayerMemberNo());
			transfer.setOrderNo(request.getTradePostNo());
			transfer.setPayeeAccount(request.getPayeeAcctNo());
			transfer.setPayerAccount(request.getPayerAcctNo());
			transfer.setPaymentNo(request.getPayPostNo());
			transfer.setTallyType(TallyType.AUTO);
			transfer.setTransOrderNo(tradeNo);
			transfer.setTallySource("OTHER");

			CancelAccTask task=new CancelAccTask(handelConfig,payOrderDao,accountService,
					 payOrder,transfer,request.isAccPasword());
			TaskHandleManger tpm = TaskHandleManger.getInstance();//任务处理线程 暂用单线程处理任务队列
			 tpm.addTask(task);//增加到任务队列
 
	}

}
